<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;

class CreateQualificationsTable extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create('qualifications', function (Blueprint $table) {
            $table->id()->from(config('migration.from'));

            $table->bigInteger('profile_id')->unsigned();
            $table->foreign('profile_id')->references('id')->on('profiles')->onDelete('cascade')->onUpdate('cascade');

            $table->bigInteger('moderator_id')->unsigned()->nullable();
            $table->foreign('moderator_id')->references('id')->on('profiles')->onDelete('set null')->onUpdate('cascade');

            $table->string('stage');

            $table->string('remark')->nullable();

            $table->jsonb('conditions')->default(json_encode([]))->comment('Соглашения');

            $table->string('status_key')->unsigned()->comment('Статус квалификации');
            $table->foreign('status_key')->references('key')->on('statuses')->onDelete('restrict')->onUpdate('restrict');

            $table->timestamp('qualified_at')->nullable();
            $table->timestamp('unqualified_at')->nullable();

            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('qualifications');
    }
}
